package com.test.learnkotlin.widget

import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView

/*

    kotlin 中 @JvmStatic 注解的作用。
    https://blog.csdn.net/qq_33404903/article/details/89425671


    Calling Kotlin from Java
    https://kotlinlang.org/docs/java-to-kotlin-interop.html
 */
object DataBindingAdapter {
    @JvmStatic
    @BindingAdapter("backgroundResId")
    fun setbackgroundResId(viewGroup: ViewGroup, @DrawableRes resId: Int) {
        if (resId == 0) {
            viewGroup.background = null
        } else {
            viewGroup.setBackgroundResource(resId)
        }
    }

    @JvmStatic
//    @BindingAdapter(value = ["android:paddingLeft", " android:paddingTop", "android:paddingRight", "android:paddingBottom"], requireAll = false)
    //报错：Application namespace for attribute  android:paddingTop will be ignored.
    //    public static final void setPadding(@org.jetbrains.annotations.NotNull
//    @BindingAdapter(value = ["android:paddingLeft", " android:paddingTop", "android:paddingRight", "android:paddingBottom"], requireAll = false)


    //Android Data Binding 掉坑日志  https://www.jianshu.com/p/5f160fe792ad


    //下面将命名空间去掉，解决上面报错问题
    @BindingAdapter(value = ["paddingLeft", "paddingTop", "paddingRight", "paddingBottom"], requireAll = false)
    fun setPadding(viewGroup: ViewGroup, left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0) {
        viewGroup.setPadding(left, top, right, bottom)
    }

//    @JvmStatic
//    @BindingAdapter("android:paddingLeft")
//    fun setPaddingLeft(viewGroup: ViewGroup, left: Int = 0) {
//        viewGroup.setPadding(
//            left, viewGroup.paddingTop, viewGroup.paddingRight, viewGroup.paddingBottom
//        )
//    }


    @JvmStatic
    @BindingAdapter("adapter")
//    fun setRecyclerViewAdapter(recyclerView: RecyclerView, adapter: RecyclerView.Adapter<*>?) {//http://www.javashuo.com/article/p-pveedber-kh.html
    fun setRecyclerViewAdapter(recyclerView: RecyclerView, adapter: RecyclerView.Adapter<*>) {
        recyclerView.adapter = adapter
    }

}